package net.yrom.screenrecorder.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import net.yrom.screenrecorder.rtmp.RESFlvData;
import net.yrom.screenrecorder.rtmp.RESFlvDataCollecter;
import net.yrom.screenrecorder.task.RtmpStreamingSender;
import net.yrom.screenrecorder.task.ScreenRecorder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ScreenRecordService extends Service {

    private MediaProjectionManager mediaProjectionManager;
    private ScreenRecorder screenRecorder;
    private RtmpStreamingSender rtmpStreamingSender;
    private String rtmpUrl;
    private ExecutorService executorService;
    private Intent intent;

    @Override
    public void onCreate() {
        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        intent = mediaProjectionManager.createScreenCaptureIntent();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        boolean record = intent.getBooleanExtra("record", false);
        if (record) {
            rtmpUrl = intent.getStringExtra("rtmpUrl");
            this.startCapture();
        } else {
            this.stopCapture();
        }
        return START_NOT_STICKY;
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void startCapture() {
        // 执行开始捕获屏幕的逻辑
        if (rtmpUrl != null) {
            MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, intent);
            if (mediaProjection != null) {
                rtmpStreamingSender = new RtmpStreamingSender(rtmpUrl);
                rtmpStreamingSender.sendStart();
                RESFlvDataCollecter collecter = new RESFlvDataCollecter() {
                    @Override
                    public void collect(RESFlvData flvData, int type) {
                        rtmpStreamingSender.sendFood(flvData, type);
                    }
                };
                screenRecorder = new ScreenRecorder(collecter, RESFlvData.VIDEO_WIDTH, RESFlvData.VIDEO_HEIGHT, RESFlvData.VIDEO_BITRATE, 1, mediaProjection);
                screenRecorder.start();
                executorService = Executors.newCachedThreadPool();
                executorService.execute(rtmpStreamingSender);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopCapture();
    }

    private void stopCapture() {
        // 执行停止捕获屏幕的逻辑
        if (screenRecorder != null) {
            screenRecorder.quit();
            screenRecorder = null;
        }
        if (rtmpStreamingSender != null) {
            rtmpStreamingSender.sendStop();
            rtmpStreamingSender.quit();
            rtmpStreamingSender = null;
        }
        if (executorService != null) {
            executorService.shutdown();
            executorService = null;
        }
    }

}
